package com.jsbb.controller;

import com.jsbb.common.Const;
import com.jsbb.common.Message;
import com.jsbb.dto.LoginDto;
import com.jsbb.dto.DataUpdateDto;
import com.jsbb.dto.PwdUpdateDto;
import com.jsbb.entity.sys.SysUser;
import com.jsbb.service.sys.SysMenuService;
import com.jsbb.service.sys.SysUserService;
import com.jsbb.util.RequestUtils;
import com.jsbb.util.TokenUtils;
import com.jsbb.vo.LoginVo;
import com.jsbb.vo.sys.SysMenuVo;
import com.jsbb.vo.sys.SysUserDetailVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 登录Controller
 *
 * @author jsbb
 */
@RestController
@Tag(name = "公共模块")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("/login")
    @Operation(summary = "登录")
    public Message<LoginVo> login(@Validated @RequestBody LoginDto loginDto, HttpServletRequest request, HttpServletResponse response) {
        SysUser userDto = sysUserService.selectByUserName(loginDto.getUserName());
        if (userDto == null) {
            return Message.error("用户名不存在");
        }
        if (!BCrypt.checkpw(loginDto.getPwd(), userDto.getPwd())) {
            return Message.error("密码错误");
        }
        LoginVo loginVo = new LoginVo();
        //设置用户信息
        SysUserDetailVo userDetail = new SysUserDetailVo();
        BeanUtils.copyProperties(userDto, userDetail);
        loginVo.setSysUser(userDetail);
        //获取用户权限
        List<SysMenuVo> menuVoList = sysMenuService.selectByUserId(userDto.getId());
        loginVo.setSysMenu(menuVoList);
        //生成token
        loginVo.setToken(TokenUtils.createToken(userDto.getId()));
        //token写入cookie
        RequestUtils.setCookie(response, Const.COOKIE_NAME, loginVo.getToken());
        request.setAttribute(Const.TOKEN, loginVo.getToken());
        return Message.success(loginVo);
    }

    @PostMapping("/logout")
    @Operation(summary = "退出")
    public Message logout(HttpServletRequest request, HttpServletResponse response) {
        RequestUtils.deleteCookie(response, Const.COOKIE_NAME);
        return Message.success();
    }

    @PostMapping("/data/update")
    @Operation(summary = "修改个人资料")
    public Message dataUpdate(@Validated @RequestBody DataUpdateDto updateDto) {
        sysUserService.dataUpdate(updateDto);
        return Message.success();
    }

    @PostMapping("/pwd/update")
    @Operation(summary = "修改密码")
    public Message pwdUpdate(@Validated @RequestBody PwdUpdateDto updateDto) {
        sysUserService.pwdUpdate(updateDto);
        return Message.success();
    }
}
